package windowFactoryPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DownloadWindow extends WindowGenerator{
	Query q = new Query();
	//Declare window components
	static JLabel jlblAccept;
	static JLabel jlblOutput = new JLabel("You are about to download __ProgName__");
	private JCheckBox jcbAcceptBox;
	static JButton jbtDownload, jbtViewLiscence;
	private JPanel centerPanel, southPanel, labelPanel, checkPanel;
	FileDownloader fd = new FileDownloader();
	
	public DownloadWindow(){
		//Set layout for parent JFrame
		setLayout(new BorderLayout());
		
		//Instantiate centerPanel
		centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(2,0,10,10));
		
		//Instantiate labelPanel and instantiate and add panel components -- Notice label
		labelPanel = new JPanel();
		labelPanel.setBorder(BorderFactory.createEtchedBorder());

		labelPanel.add(jlblOutput);
		
		//add labelPanel to centerPanel
		centerPanel.add(labelPanel);
		
		//Instantiate checkPanel and instantiate and add panel components -- Checkbox and Label
		checkPanel = new JPanel();
		checkPanel.setBorder(BorderFactory.createEtchedBorder());
		jcbAcceptBox = new JCheckBox();
		checkPanel.add(jcbAcceptBox);
		jlblAccept = new JLabel("By clicking you agree to Terms and Conditions in all liscensing information.");
		checkPanel.add(jlblAccept);
		
		//add checkPanel to centerPanel
		centerPanel.add(checkPanel);
		
		//add centerPanel to parent JFrame.CENTER
		add(centerPanel, BorderLayout.CENTER);
		
		//Instantiate southPanel and instantiate and add panel components -- download Button and liscences Button
		southPanel = new JPanel();
		jbtDownload = new JButton("Download");
		jbtDownload.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(jcbAcceptBox.isSelected()){
					FileDownloader.filePath = "";
					FileDownloader.fileName = "";
					FileDownloader.fileSize = 0;
					q.downloadCredentials();
					if(Query.noDownload == false){
						SwingUtilities.invokeLater(new Runnable(){
							public  void run() {
								fd.fileDown();
							}
						});
					}
				}else{
					JOptionPane.showMessageDialog(null, "You must agree to the Terms and Conditions\n" +
							"before you may start downloading.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		southPanel.add(jbtDownload);
		jbtViewLiscence = new JButton("View Liscences");
		jbtViewLiscence.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.viewLiscencingInfo();
				WindowGenerator.generateLiscencesWindow();
			}
		});
		southPanel.add(jbtViewLiscence);
		
		//add southPanel to parent JFrame.SOUTH		
		add(southPanel, BorderLayout.SOUTH);
		
	}
}
